﻿using System.Data.Entity.ModelConfiguration;
using CH.FourBoards.LagerWalti.Persistence.Entities;

namespace CH.FourBoards.LagerWalti.Persistence.Mapping
{
    internal class CategoryMap : EntityTypeConfiguration<Category>
    {
        internal CategoryMap()
        {
            #region Primary Key and Row Version

            HasKey(t => t.Id);

            // RowVersion for optimistic locking
            //Property(t => t.Version).IsRowVersion();

            #endregion

            #region Foreign Keys

            Property(t => t.ParentCategoryId).IsOptional();

            #endregion

            #region Properties

            Property(t => t.Name).IsRequired().HasMaxLength(500);
            Property(t => t.Image).IsOptional().HasMaxLength(500);

            #endregion

            #region Relationships

            HasOptional(t => t.ParentCategory).WithMany().HasForeignKey(t => t.ParentCategoryId);
            HasMany(t => t.Products).WithOptional().HasForeignKey(t => t.CategoryId);

            #endregion
        }
    }
}
